<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            @click="goAddGoods"
          >添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table
        :data="goodslist"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="#"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品重量"
          width="90"
        >
        </el-table-column>
        <el-table-column
          prop="upd_time"
          label="创建时间"
          width="180"
        >
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      token: '',
      queryInfo: {
        'query': '',
        'pagenum': 1,
        'pagesize': 5
      },
      total: 0,
      goodslist: [],
      addDialogVisible: false
    }
  },
  created() {
    this.token = window.sessionStorage.getItem('token')
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryInfo,
        headers: { 'Authorization': this.token }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // console.log(res)
      this.goodslist = res.data.goods
      // 总条数
      this.total = res.data.total
    },
    // 去添加商品 详情
    goAddGoods() {
      // 跳转路由
      this.$router.push('/addGoods')
    },
    // 条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 删除
    async removeGoods(row) {
      // console.log(row.goods_id)
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.error('已取消删除！')
      }

      const { data: res } = await this.$axios.delete(`goods/${row.goods_id}`, {
        headers: { 'Authorization': this.token }
      })

      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>